//
//  TGPlanTopCollectionViewCell.m
//  WanCaiC8
//
//  Created by QiuXun on 2019/1/17.
//  Copyright © 2019 tangang. All rights reserved.
//

#import "TGPlanTopCollectionViewCell.h"
#import "LotteryNumCell.h"


@interface TGPlanTopCollectionViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,strong) UICollectionView  *collectionView;
@property (nonatomic ,strong) UICollectionView  *bottomCollectionView;

@end
@implementation TGPlanTopCollectionViewCell{
    QXLable     *labTime;
    UIView  *lin2;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        
    }
    return self;
}

-(void)setupUI{
    
    labTime=[[QXLable alloc]init];
    
    labTime.textAlignment=NSTextAlignmentCenter;
    [self.contentView addSubview:labTime];
    [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.centerY.equalTo(self.contentView);
        make.width.mas_equalTo(@((SCREEN_WIDTH-1.0)/4.0));
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTLINECOLOR;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.height.mas_equalTo(@.7);
    }];
    
    UIView  *lin1=[[UIView alloc]init];
    lin1.backgroundColor=DEFAULTLINECOLOR;
    [self.contentView addSubview:lin1];
    [lin1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(@.7);
    }];
    
    lin2=[[UIView alloc]init];
    lin2.backgroundColor=DEFAULTLINECOLOR;
    [self.contentView addSubview:lin2];
    [lin2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.equalTo(self.contentView);
        make.left.equalTo(labTime.mas_right);
        make.width.mas_equalTo(@.7);
    }];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lin2).with.offset(10);
        make.top.equalTo(self.contentView).with.offset(10);
        make.right.equalTo(self.contentView);
        make.height.mas_equalTo(@(real_size(25)));
    }];
    
    [self.contentView addSubview:self.bottomCollectionView];
    [self.bottomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lin2).with.offset(10);
        make.bottom.equalTo(self.contentView).with.offset(-10);
        make.right.equalTo(self.contentView);
        make.top.equalTo(self.collectionView.mas_bottom);
        make.height.mas_equalTo(@0);
    }];
    
    
}


- (void)setModel:(RecommendModel *)model{
    _model=model;
    if ([model.OpenTime isEqualToString:@"01-01 00:00"]) {
        NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@期",model.Issue]];
        labTime.attributedText=attStr;
    }else{
        NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@期\n%@",model.Issue,model.OpenTime]];
        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(0, model.Issue.length+1)];
        
        labTime.attributedText=attStr;
    }
    
    if (model.OpenNumAliasArr.count>0) {
        [self.bottomCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(20)));
        }];
    }else{
        [self.bottomCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
        }];
    }
    [self.collectionView reloadData];
    [self.bottomCollectionView reloadData];
}

- (void)setIsNew:(BOOL)isNew{
    _isNew=isNew;
    
    
    if (isNew) {
        labTime.text=[NSString stringWithFormat:@"%@期%@计划",self.model.Issue,self.model.name];
        //        labTime.attributedText=nil;
        labTime.textColor=NAVCOLOR;
        [labTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).with.offset(10);
            make.center.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).with.offset(-10);
        }];
        lin2.hidden=YES;
        self.collectionView.hidden=YES;
        self.bottomCollectionView.hidden=YES;
    }
    
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.isNew) {
        return 0;
    }
    if (collectionView==self.collectionView) {
        if (self.model.lType==65) {
            return self.model.PCArr.count;
        }else if ([[BaseVM manage]getType:self.model.lType]==LotteryNumTypeLottery){
            return self.model.LotteryArr.count;
        }
        return self.model.OpenNumArr.count;
    }
    return self.model.OpenNumAliasArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LotteryNumCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LotteryNumCell" forIndexPath:indexPath];
    if (collectionView==self.collectionView) {
        LotteryNumType type=[[BaseVM manage]getType:self.model.lType];
        if (self.model.lType==65) {
            [cell setTitle:self.model.PCArr[indexPath.row] andType:type];
        }else if (type==LotteryNumTypeLottery){
            [cell setTitle:self.model.LotteryArr[indexPath.row] andType:type];
        }else{
            [cell setTitle:self.model.OpenNumArr[indexPath.row] andType:type];
        }
        if ((type==LotteryNumTypeChromosphere||type==LotteryNumTypeBrings||type==LotteryNumTypeSevenLecai)&&(indexPath.row==self.model.OpenNumArr.count-1)) {
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }else if (type==LotteryNumTypeBrings&&(indexPath.row==self.model.OpenNumArr.count-2)){
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }
    }else{
        [cell setDownTitle:self.model.OpenNumAliasArr[indexPath.row] andType:[[BaseVM manage]getType:self.model.lType]];
    }
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 2, 0, 3);
}

#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(real_size(25), real_size(25))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _collectionView;
}

- (UICollectionView *)bottomCollectionView{
    if (_bottomCollectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(real_size(25), real_size(20))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _bottomCollectionView.delegate = self;
        _bottomCollectionView.dataSource = self;
        _bottomCollectionView.backgroundColor=[UIColor clearColor];
        _bottomCollectionView.showsVerticalScrollIndicator=NO;
        [_bottomCollectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _bottomCollectionView;
}


@end
